const Cloud = require('wx-server-sdk');
const TcbRouter = require('tcb-router');

Cloud.init({
    env: Cloud.DYNAMIC_CURRENT_ENV
});

const db = Cloud.database();
const _ = db.command;
const $ = db.command.aggregate;

const getParams = ctx => {
    let params = ctx._req.event;
    delete params.$url;

    return params;
};

const success = data => {
    return {
        status: 200,
        data
    };
};

// 云函数入口函数s
exports.main = async (event, context) => {
    const app = new TcbRouter({ event });

    app.router('get_user_openid', getUserOpenId);

    return app.serve();
};

// 获取用户信息
async function getUserOpenId(ctx, next) {
    const wxContext = Cloud.getWXContext();

    ctx.body = success({
        openid: wxContext.OPENID,
        appid: wxContext.APPID,
        unionid: wxContext.UNIONID,
    })
}
